import request from '@/utils/request'
import layout from '@/layout'
export function login(data) {
  return request({
    url: '/users/login',
    method: 'post',
    data
  })
}

export function getInfo(token) {
  return request({
    url: '/users/info',
    method: 'get',
    params: { token }
  })
}

export function logout() {
  return request({
    url: '/users/logout',
    method: 'get'
  })
}

// 根据角色获取菜单
export async function getMenusAsync(roleIds) {
  const res = await request({
    url: '/menus/' + roleIds,
    method: 'get'
  })
  return res
}
// 获取根菜单
export function rootmenus(menulist) {
  // 定义一个空数组，这个是用来装真正路由数据的
  const menuRouters = []
  // 先取出根节点，没有父id的就是根节点
  menulist.forEach((m, i) => {
    if (m.parentId === 0) {
      m.fullPath = '/' + m.path
      const module = {
        path: '/' + m.path,
        component: layout,
        name: m.path,
        meta: { id: m.id, title: m.title, fullPath: '/' + m.path }
        // children: [
        //   {
        //     path: '',
        //     component: () => import('@/views/' + m.path + '/index'),
        //     meta: {
        //       menuHide: true,
        //       title: m.title
        //     }
        //   }
        // ]
      }
      menuRouters.push(module)
    }
  })
  return menuRouters
}
// 递归
export function convertTree(roots, actions) {
  roots.forEach(r => {
    actions.forEach((m, i) => {
      if (m.parentId && m.parentId === r.meta.id) {
        if (!r.children) r.children = []
        m.fullPath = r.meta.fullPath + '/' + m.path
        const menu = {
          path: m.path,
          name: m.path,
          component: (resolve) => require([`@/views/${m.path}/index`], resolve),
          // component: () => import('@/views' + '/' + m.path + '/index'),
          meta: { id: m.id, title: m.title, fullPath: r.meta.fullPath + '/' + m.path }
        }
        r.children.push(menu)
      }
    })
    if (r.children) {
      convertTree(r.children, actions)
    }
  })
}
